package demo;

import javax.media.opengl.*;
import jocode.*;

/**
 * <P>
 * napier at potatoland dot org
 */
public class DemoMultiTexture extends JOApp {
    // Handles for textures
    int escherTxtr = 0;
    int grassTxtr = 0;
    int bloodTxtr = 0;

    /**
     * Run main loop of application.  Handle mouse and keyboard events.
     */
    public static void main(String args[]) {
    	DemoMultiTexture demo = new DemoMultiTexture();
    	displayWidth = 800;
    	displayHeight = 600;
        demo.run();
    }

    /**
     * Initialize the scene.  Called by GLApp.run()
     */
    public void setup()
    {
        // Create three textures
    	escherTxtr = makeTexture("images/escher_pattern.png");
    	grassTxtr = makeTexture("images/grass_1_512.jpg");
    	bloodTxtr = makeTexture("images/spotlight_200_gray.png");  //spotlight_200_inverse.png");  //eye.jpg");

        // MULTI-TEXTURE: prepare multitexture for use
        JOMultiTexture.init();

        // setup perspective
        setPerspective();

        // Create a point light (white)
        setLight( GL.GL_LIGHT1,
        		new float[] { 1.0f, 1.0f, 1.0f, 1.0f },   // diffuse color
        		new float[] { .6f, 0.6f, 0.6f, 1.0f },    // ambient
        		new float[] { 1.0f, 1.0f, 1.0f, 1.0f },   // specular
        		new float[] { -5f, 4f, 3f, 0f }           // position
        		);
        
        // material color
        setMaterial(
        		new float[] { 1.0f, 1.0f, 1.0f, 1.0f },   // diffuse color
        		new float[] { 0.9f, 0.9f, 0.9f, 1.0f },   // ambient
        		new float[] { 1.0f, 1.0f, 1.0f, 1.0f },   // specular
        		new float[] { 0.0f, 0.0f, 0.0f, 0.0f },   // emissive
        		110f);        // shininess

        // no overall scene lighting
        setAmbientLight(new float[] { .5f, .5f, .5f, 1f });

        // enable lighting and texture rendering
        gl.glEnable(GL.GL_LIGHTING);
        gl.glEnable(GL.GL_TEXTURE_2D);
    }

    /**
     * Render one frame.  Called by GLApp.run().
     */
    public void draw() {
        // clear depth buffer and color
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

        setPerspective();
        
        // select model view for subsequent transforms
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();

        // set the viewpoint
        glu.gluLookAt(0f, 0f, 20f, // where is the eye
        			0f, 0f, 0f,    // what point are we looking at
        			0f, 1f, 0f);   // which way is up

        //------------------------------------------------------
        // MULTI-TEXTURE: draw quads with multiple textures
        //------------------------------------------------------

        // row 1
        
        // activate two texture layers
        JOMultiTexture.activateTexture(0,escherTxtr);
        JOMultiTexture.activateTexture(1,grassTxtr);
        JOMultiTexture.drawQuadMT(-9,0,0, 6,6);

        // activate two texture layers
        JOMultiTexture.activateTexture(0,escherTxtr);
        JOMultiTexture.activateTexture(1,bloodTxtr);
        JOMultiTexture.drawQuadMT(-3,0,0, 6,6);

        // activate two texture layers
        JOMultiTexture.activateTexture(0,grassTxtr);
        JOMultiTexture.activateTexture(1,bloodTxtr);
        JOMultiTexture.drawQuadMT(3,0,0, 6,6);

        // row 2

        // activate three texture layers
        JOMultiTexture.activateTexture(0,escherTxtr);
        JOMultiTexture.activateTexture(1,grassTxtr);
        JOMultiTexture.activateTexture(2,bloodTxtr);
        JOMultiTexture.drawQuadMT(-9,-6,0, 6,6);

        // disable all but the first layer (so regular textures draw correctly)
        JOMultiTexture.disableTexture(1);
        JOMultiTexture.disableTexture(2);
        JOMultiTexture.drawQuadMT(-3,-6,0, 6,6);

        // reset the default layer (0) back to texture1 (so regular textures draw correctly)
        JOMultiTexture.activateTexture(0,grassTxtr);
        JOMultiTexture.drawQuadMT(3,-6,0, 6,6);
    }


    public void mouseUp(int x, int y) {
    }


    public void keyDown(int keycode) {
    }


    public void keyUp(int keycode) {
    }
}